package com.china08.yxyapi.api.home.resource;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter;
import com.china08.yxyapi.entity.mg.user.YxyUserCenter.ResSubjectPhase;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyUserCenterRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ResourceGetRecordController extends YxyApiBaseController {

    @Autowired
    private YxyUserCenterRepository yxyUserCenterRepository;

    @ApiOperation(value = "获取")
    @ResponseBody
    @RequestMapping(value = "/api_yw/home/res/record", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, RespModel model)
            throws IllegalAccessException, InvocationTargetException {
        String userId = getProfile(request, response).getId();
        YxyUserCenter center = yxyUserCenterRepository.findByUserId(userId);
        if (center != null) {
            ResSubjectPhase lastWeike = center.getLastResView();
            if (lastWeike != null) {
                model.phase = lastWeike.getPhaseId();
                model.phaseName = lastWeike.getPhaseName();
                model.bookId = lastWeike.getBookId();
                model.subject = lastWeike.getSubjectId();
                model.subjectName = lastWeike.getSubjectName();
                model.version = lastWeike.getVersionId();
                model.versionName = lastWeike.getVersionName();
                model.semester = lastWeike.getSemester();
                model.semesterName = lastWeike.getSemesterName();
            }
        }
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String phase = "03";
        public String phaseName = "高中";
        public String subject = "jcsub13";
        public String subjectName = "物理";
        public String version = "bb08";
        public String versionName = "人教课标版";
        public String semester = "Bb45";
        public String semesterName = "必修1";
        public String bookId = "67933";
    }

}
